<template>
  <div>
    <el-form :inline="true" :model="search" class="demo-form-inline">
      
      <el-row class="table-head-box">
         <el-col :span="4" class="flex flex-start">
          <el-button type="primary" icon="el-icon-plus"    plain @click="addCommonWordsHandler()">添加常用语</el-button>
        </el-col>
        <el-col :span="20" class="flex flex-end input-box">
          <el-input v-model="search.lkParam.title_name" clearable placeholder="标题"></el-input>
          <el-button type="primary" icon="el-icon-search"  @click="searchHandler">查询</el-button>
        </el-col>

      </el-row>
    </el-form>
    <el-table :data="commonWordsList" border style="width: 100%" tooltip-effect="dark" size="mini">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column  prop="titleId" label="编号" >
      </el-table-column>
      <el-table-column  prop="titleName" label="标题名称" >
      </el-table-column>
      <el-table-column prop="titleDesc" label="标题描述">
      </el-table-column>
      <el-table-column prop="status" label="状态">
         <template slot-scope="scope">
         <el-switch
          v-model="scope.row.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="2"
          @change="updateStatusHandler(scope.row)"
          >
        </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.userName !='sys'" class="el-icon-edit" type="primary"  size="mini" @click="updateCommonWordsHandler(scope.$index, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-row>
        <el-col :span="24" style="text-align:right;">
            <el-pagination background layout="total,prev, pager, next" :page-count="page.size" :total="page.total" @current-change="sizeChangeHandler"> </el-pagination>
        </el-col>
    </el-row> -->
    <CommonWordsAdd :commonWordsDialog="showCommonWordsDialog" :popCommonWords="commonWords"  @close="closeHandler"></CommonWordsAdd>
  </div>
</template>
<script>
import {commonWordsListUrl,commonWordsSaveUrl} from '@/utils/common'
import CommonWordsAdd from "./common-words-add";

export default {
  components: {
    CommonWordsAdd
  },
  data() {
    return {
      commonWordsList: [],
      commonWords: null,
      showCommonWordsDialog: false,
      search: {
        current:1,
        size:10,
        total: 0,
        lkParam: {titleName:""},
        eqParam:{},
      }
    };
  },
  mounted() {
    this.loadCommonWordsList();
  },
  methods: {
    addCommonWordsHandler: function () {
      this.commonWords = {status:1};
      this.showCommonWordsDialog = true;
    },
    updateCommonWordsHandler:function(index,row){
      this.commonWords = row;
      this.showCommonWordsDialog = true;
    },
    updateStatusHandler: function (row) {
      this.$confirm("确认无效吗, 是否继续?", "提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" })
        .then(() => {
          this.axios
            .post(commonWordsSaveUrl,row)
            .then(response => {
              console.log(response)
              this.$message({ type: "success", message: "设置无效成功!"  });
              this.loadCommonWordsList();
            })
            .catch(response => {
                console.log(response)
              this.$message({ type: "warn", message: "设置无效失败!"  });
            });
        })
        .catch(() => {
          this.$message({ type: "info", message: "已取消设置无效" });
        });
    },
    closeHandler: function () {
      this.showCommonWordsDialog = false;
      this.loadCommonWordsList();
    },
    /**
     * 删除用户
     */
    // deleteCommonWordsHandler: function (index, row) {
    //   this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" })
    //     .then(() => {
    //       this.axios
    //         .post(replaceUrlParam(commonWordsDelUrl,[row.titleId]))
    //         .then(response => {
    //             console.log(response)
    //           this.$message({ type: "success", message: "删除成功!"  });
    //           this.loadCommonWordsList();
    //         })
    //         .catch(response => {
    //             console.log(response)
    //           this.$message({ type: "warn", message: "删除失败!"  });
    //         });
    //     })
    //     .catch(() => {
    //       this.$message({ type: "info", message: "已取消删除" });
    //     });
    // },
    loadCommonWordsList() {
      this.axios
        .post(commonWordsListUrl, this.search.lkParam)
        .then(response => {
          var data = response.data.data;
          this.commonWordsList = data;
        })
        .catch(response => {
          console.log("response -   " + response);
        });
    },
    sizeChangeHandler: function (value) {
      this.page.current = value;
      this.loadCommonWordsList();
    },
    searchHandler: function () {
      this.loadCommonWordsList();
    }
  }
};
</script>
<style scoped>
.grid-content {
  text-align: left;
  height: 45px;
}
</style>
